package com.xk.androiddemo.ui.dashboard;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;


import com.kongzue.dialogx.dialogs.PopNotification;
import com.xk.androiddemo.databinding.FragmentDashboardBinding;
import com.xk.androiddemo.model.MonitorDataItem;


public class DashboardFragment extends Fragment {

    private FragmentDashboardBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        DashboardViewModel dashboardViewModel =
                new ViewModelProvider(this).get(DashboardViewModel.class);
        binding = FragmentDashboardBinding.inflate(inflater, container, false);
        binding.iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.tvSelect.setText("设备1");
                dashboardViewModel.createData();
            }
        });
        binding.iv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.tvSelect.setText("设备2");
                dashboardViewModel.createData();
            }
        });
        binding.iv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.tvSelect.setText("设备3");
                dashboardViewModel.createData();
            }
        });
        binding.iv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.tvSelect.setText("设备4");
                dashboardViewModel.createData();
            }
        });
        binding.iv5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.tvSelect.setText("设备5");
                dashboardViewModel.createData();
            }
        });
        dashboardViewModel.data.observe(getActivity(), new Observer<MonitorDataItem>() {
            @Override
            public void onChanged(MonitorDataItem monitorDataItem) {
                binding.tvPress.setText(monitorDataItem.getGasPressure()+"");
                binding.tv2.setText(monitorDataItem.getFlowRate()+"");
                binding.tv3.setText(monitorDataItem.getCurrent()+"");
                if(monitorDataItem.isAlarmSet()){
                    binding.tv4.setText("开");
                    if(monitorDataItem.getValvePosition()){
                        PopNotification.show("阀门未关");
                    }
                }else{
                    binding.tv4.setText("关");
                }
                if(monitorDataItem.getValvePosition()){
                    if(monitorDataItem.getGasPressure()>49.8){
                        PopNotification.show("液位过高");
                    }
                }
                binding.tv5.setText(monitorDataItem.getFlowRate()+"");
                binding.tv6.setText(monitorDataItem.getVoltage()+"");
                if(monitorDataItem.getValvePosition()){
                    binding.tv7.setText("开");
                }else{
                    binding.tv7.setText("关");
                }
            }
        });
        View root = binding.getRoot();
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}